import 'dart:io';

import 'package:lolo/auxiliary/folder.dart';

/// 在Flutter中也有一个变量`kDebugMode`
/// 但它是`bool.fromEnvironment('dart.vm.product')`
/// 而我不知道dart VM中有什么环境变量，所以这个方式不可控。
bool get devMode {
  bool dev = false;
  assert(dev = true);
  return dev;
}

String get pwd => Directory.current.path;

String? findPath(String name) {
  //如果搜索到了，结果里会换行，需要去掉
  String result = Process.runSync("locate", ["-l1", name]).stdout;
  return result.isEmpty ? null : result.split("\n")[0];
}

Directory findFolder(String name) {
  String? path = findPath(name);
  if (path != null) return Directory(path);
  throw Exception("没有找到文件夹：$name");
}

extension Child on Directory {
  Directory childFolder(String name) {
    for (final sub in subFolders) {
      if (sub.name == name) return sub;
    }
    throw Exception("没有找到子文件夹：$name");
  }
}
